local function split(str, delimiter)
    local result = {}
    for match in (str .. delimiter):gmatch("(.-)" .. delimiter) do
        table.insert(result, match)
    end
    return result
end

--不匹配大括号内的逗号
local function splitString(input)
    local result = {}
    local currentPart = ""

    local insideBrackets = 0

    for i = 1, #input do
        local char = input:sub(i, i)

        if char == "{" then
            insideBrackets = insideBrackets + 1
        elseif char == "}" then
            insideBrackets = insideBrackets - 1
        end

        if char == "," and insideBrackets == 0 then
            table.insert(result, currentPart)
            currentPart = ""
        else
            currentPart = currentPart .. char
        end
    end

    table.insert(result, currentPart)

    return result
end

function ParseList(str)
    local result = {}
    str = str:sub(2, -2) -- 去掉两端的大括号
    str = str:gsub('，', ',')
    --按照逗号分割
    local items = splitString(str)

    local function get_data(value)
        if tonumber(value) then
            return tonumber(value)
        elseif value:lower() == "true" then
            return true
        elseif value:lower() == "false" then
            return false
        elseif value:match("^%b{}$") then -- 匹配大括号包围的内容
            return ParseList(value)
        else
            return value
        end
    end

    for _, item in ipairs(items) do
        item = item:match("^%s*(.-)%s*$") -- 去掉首尾空格
        --如果是无key的table
        if item:match("^%b{}$") then
            result[#result + 1] = get_data(item)
        else
            local key, value = item:match("([^=]+)=(.+)")
            if key and value then
                result[key] = get_data(value)
            else
                result[#result + 1] = get_data(item)
            end
        end
    end
    return result
end

function ParseString(input)
    local result = {}
    local elements = split(input, "|n")

    local function get_data(value)
        if tonumber(value) then
            return tonumber(value)
        elseif value:lower() == "true" then
            return true
        elseif value:lower() == "false" then
            return false
        elseif value:match("^%b{}$") then -- 匹配大括号包围的内容
            return ParseList(value)
        else
            return value
        end
    end

    for _, element in ipairs(elements) do
        --如果是无key的table
        if element:match("^%b{}$") then
            result[#result + 1] = get_data(element)
        else
            local key, value = element:match("([^=]+)=(.+)")
            if key and value then
                result[key] = get_data(value)
            else
                result[#result + 1] = get_data(element)
            end
        end
    end

    return result
end

function LineToTable(input)
    return ParseString(input)
end

-----------------额外支持target_type----------------------------------
function FormatTargetType(key, str)
    if key ~= 'target_type' then
        return str
    end

    str = str:gsub('无目标', '0')
    str = str:gsub('单位目标', '1')
    str = str:gsub('点目标', '2')
    str = str:gsub('单位或点', '3')

    return str
end

---@param Start any
---@param mergeRow Csharp.Row
---@param pattern_start any
---@param pattern_end any
function MergeFunc(Start, mergeRow, pattern_start, pattern_end)
    local End = Start
    -- 找到合并单元格的结尾
    for n = Start, mergeRow.LastCellNum do
        if mergeRow:GetCell(n).StringCellValue:find(pattern_end) then
            End = n
            break
        end
    end

    local count = End - Start
    local a, b = mergeRow:GetCell(Start).StringCellValue:find(pattern_start)
    local key = mergeRow:GetCell(Start).StringCellValue:sub(1, a - 1)

    return count, key
end

return function(name)
    return require('include.book_type.' .. name)
end
